package com.wei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wei.entity.GdComplain;
import com.wei.entity.GdHousehold;
import com.wei.entity.GdProperty;
import com.wei.mapper.GdHouseholdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdHouseholdController {

    @Autowired
    private GdHouseholdMapper householdMapper;

    @RequestMapping("/getHousehold")
    public List<GdHousehold> getHousehold(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        List<GdHousehold> properties=householdMapper.selectByMap(map);
        return properties;
    }

    @RequestMapping("/addHousehold")
    public void setHousehold(@RequestParam Map<String,Object> householdObj){
        GdHousehold Household =setParam(householdObj);
        householdMapper.insert(Household);
    }

    @RequestMapping("/updateHousehold")
    public void updateHousehold(@RequestParam Map<String,Object> householdObj){
        GdHousehold Household = setParam(householdObj);
        householdMapper.updateById(Household);
    }

    @RequestMapping("/deleteHousehold")
    public void deleteHousehold(@RequestParam String householdId){
        householdMapper.deleteById(householdId);
    }

    /**
     * 通过住户姓名、房产地址判断该住户是否存在(通用)
     * @param householdName
     * @param propertyAddress
     * @return
     */
    @RequestMapping("/hasHousehold")
    public Boolean hasPropertyAddress(@RequestParam String householdName,@RequestParam String propertyAddress){
        QueryWrapper<GdHousehold> wrapper=new QueryWrapper<>();
        wrapper.eq("household_name",householdName);
        wrapper.eq("property_address",propertyAddress);
        wrapper.eq("household_relation","业主");
        wrapper.eq("deleted",0);
        if(householdMapper.selectOne(wrapper)==null){
            return false;
        } else {
            return true;
        }
    }

    /**
     * 通过物业地址进行记录查询，用于房产中住户信息的管理(用户)
     * @param address
     * @return
     */
    @RequestMapping("/getHouseholdByAddress")
    public List<GdHousehold> getHouseholdByAddress(@RequestParam String address){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("property_address", address);
        return householdMapper.selectByMap(map);
    }

    /**
     * 设置参数方法
     * @param householdObj
     * @return
     */
    private GdHousehold setParam(@RequestParam Map<String, Object> householdObj) {
        GdHousehold Household=new GdHousehold();
        Household.setHouseholdId((String) householdObj.get("householdId"));
        Household.setHouseholdForeignkey((String) householdObj.get("householdForeignkey"));
        Household.setHouseholdName((String) householdObj.get("householdName"));
        Household.setHouseholdSex((String) householdObj.get("householdSex"));
        Household.setPropertyAddress((String) householdObj.get("propertyAddress"));
        Household.setHouseholdRelation((String) householdObj.get("householdRelation"));
        Household.setHouseholdPhone((String) householdObj.get("householdPhone"));
        Household.setHouseholdIntime((String) householdObj.get("householdIntime"));
        Household.setHouseholdBirthplace((String) householdObj.get("householdBirthplace"));
        Household.setHouseholdIdcard((String) householdObj.get("householdIdcard"));
        Household.setHouseholdWorkplace((String) householdObj.get("householdWorkplace"));
        Household.setHouseholdWorkaddress((String) householdObj.get("householdWorkaddress"));
        Household.setHouseholdUnitemail((String) householdObj.get("householdUnitemail"));
        Household.setHouseholdBank((String) householdObj.get("householdBank"));
        Household.setHouseholdBankAccount((String) householdObj.get("householdBank_account"));
        Household.setHouseholdOuttime((String) householdObj.get("householdOuttime"));
        return Household;
    }
}

